import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      })
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    server: {
      port: 3000,
      proxy: {
        '/api': {
          target: env.VITE_APP_API_BASE_URL || 'http://localhost:8080',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
          bypass(req, res, options) {
            const proxyUrl = options.target + options.rewrite(req.url);
            console.log('代理后的地址:', proxyUrl); // 控制台打印
            res.setHeader('x-proxy-url', proxyUrl); // 设置到响应头
          }
        },
        '/uploads': {
          target: env.VITE_APP_API_BASE_URL || 'http://localhost:8080',
          changeOrigin: true,
          bypass(req, res, options) {
            const proxyUrl = options.target + req.url;
            console.log('代理后的地址:', proxyUrl); // 控制台打印
            res.setHeader('x-proxy-url', proxyUrl); // 设置到响应头
          }
        }
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
        // 使用 modern-compiler API 避免警告
        api: 'modern-compiler',
        },
      }
    }
  }
})